package com.hiof.servercommunication.receive.beans;

import com.hiof.overlays.MapPointsManager;

/**
 * @author Bergy
 */
public class GeoPoint {
//    public static final double DEFAULT_LATITUDE = 59.127522823400795;
//    public static final double DEFAULT_LONGITUDE = 11.354172136634588;
//    public static final double DEFAULT_HEIGHT = 89.70001220703125;
    private double lat;
    private double lng;
    private double altitude;
    private com.google.android.maps.GeoPoint googleGeoPoint;

    public GeoPoint() {

        this.lat = MapPointsManager.getInstane().getPoint(0).getLat(); // DEFAULT_LATITUDE;
        this.lng = MapPointsManager.getInstane().getPoint(0).getLng();//DEFAULT_LONGITUDE;
        this.altitude = MapPointsManager.getInstane().getPoint(0).getAltitude();//DEFAULT_HEIGHT;
        this.googleGeoPoint = new com.google.android.maps.GeoPoint(getLatE6(), getLngE6());
    }

    public GeoPoint(double lat, double lng, double altitude) {
        this.lat = lat;
        this.lng = lng;
        this.altitude = altitude;
        this.googleGeoPoint = new com.google.android.maps.GeoPoint(getLatE6(), getLngE6());
    }

    public double getAltitude() {
        return altitude;
    }

    public void setAltitude(double altitude) {
        this.altitude = altitude;
    }

    public double getLat() {
        return lat;
    }

    public int getLatE6() {
        return (Math.round((int) (lat * 1e6)));
    }

    protected double getLatInRadian() {
        return Math.toRadians(getLat());
    }

    public void setLat(double lat) {
        this.lat = lat;
    }

    public double getLng() {
        return lng;
    }

    public int getLngE6() {
        return (Math.round((int) (lng * 1e6)));
    }

    protected double getLngInRadian() {
        return Math.toRadians(getLng());
    }

    public void setLng(double lng) {
        this.lng = lng;
    }

    public com.google.android.maps.GeoPoint getGoogleGeoPoint() {
        if (googleGeoPoint == null) {
            this.googleGeoPoint = new com.google.android.maps.GeoPoint(getLatE6(), getLngE6());
        }
        return googleGeoPoint;
    }

    public CartesianCoordinate toCartersianCartesianCoordinate() {
        double radius = convertSeaLevelHeight2EarthRadius(this.getAltitude());

        double x = radius * Math.cos(getLatInRadian()) * Math.sin(getLngInRadian());
        double y = radius * Math.sin(getLatInRadian());
        double z = radius * Math.cos(getLatInRadian()) * Math.cos(getLngInRadian());
        return new CartesianCoordinate(x, y, z);
    }

    private double convertSeaLevelHeight2EarthRadius(double altitude) {
        return altitude + 6370000;
    }

    public boolean isEquals(GeoPoint geoPoint) {
        if (this.lat == geoPoint.getLat() && this.lng == geoPoint.getLng() && this.altitude == geoPoint.getAltitude()) {
            return true;
        } else {
            return false;
        }
    }
}
